﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;
using System.Runtime.Serialization;
using EnCor.Security;

namespace EnCor
{
    [Serializable]
    public sealed class EnCorIdentity : UserIdentity, IIdentity, ISerializable
    {
        public static readonly EnCorIdentity Anonymous = new EnCorIdentity("", "unknown", "", false);
        
        private string _AuthenticationType;

        private bool _IsAuthenticated;

        private string _Name;

        private string _UserId;

        private EnCorIdentity()
        { 
            // empty constructor just for Serializing
        }

        public EnCorIdentity(string userId, string name, string authenticationType, bool isAuthenticated)
        {
            _UserId = userId;
            _Name = name;
            _AuthenticationType = authenticationType;
            _IsAuthenticated = isAuthenticated;
        }

        protected EnCorIdentity(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new System.ArgumentNullException("info");

            _UserId = info.GetString("UserID");
            _Name = info.GetString("Name");
            _IsAuthenticated = info.GetBoolean("IsAuthenticated");
            _AuthenticationType = info.GetString("AuthenticationType");
        }



        public string UserId
        {
            get { return _UserId; }
        }

        #region IIdentity 成员

        public string AuthenticationType
        {
            get { return _AuthenticationType; }
        }

        public bool IsAuthenticated
        {
            get { return _IsAuthenticated; }
        }

        public string Name
        {
            get { return _Name; }
        }

        #endregion


        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new System.ArgumentNullException("info");
            info.AddValue("UserID", _UserId);
            info.AddValue("Name", _Name);
            info.AddValue("AuthenticationType", _AuthenticationType);
            info.AddValue("IsAuthenticated", _IsAuthenticated);
        }

        #endregion
    }
}
